package com.gxa.uhome.service.impl;

import com.gxa.uhome.mapper.StationMasterMapper;
import com.gxa.uhome.pojo.StationMaster;
import com.gxa.uhome.pojo.vo.StationMasterVo;
import com.gxa.uhome.service.StationMasterService;
import com.gxa.uhome.util.MD5Util;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author: fanyouzhao
 * @date: 11:44 2021/6/8
 */
@Transactional(rollbackFor = Throwable.class)
@Service("stationMasterService")
public class StationMasterServiceImpl implements StationMasterService {
    @Resource
    private StationMasterMapper stationMasterMapper;

    @Override
    public List<StationMasterVo> selectAll() {
        return stationMasterMapper.selectAll();
    }

    @Override
    public List<StationMasterVo> selectByPage(Integer page, Integer size) {
        page = page!=null && page>=1 ? (page - 1) * size : 0;
        return stationMasterMapper.selectAllByPage(page, size);
    }

    @Override
    public List<StationMasterVo> selectByStationNameByPage(String stationName, Integer page, Integer size) {
        page = page!=null && page>=1 ? (page - 1) * size : 0;
        return stationMasterMapper.selectByStationNameByPage(stationName, page, size);
    }

    @Override
    public List<StationMasterVo> selectLikeNameOrPhoneByPage(String content, Integer page, Integer size) {
        content = content==null || "".equals(content) ? "%%" : "%"+content+"%";
        page = page!=null && page>=1 ? (page - 1) * size : 0;
        return stationMasterMapper.selectLikeNameOrPhoneByPage(content, page, size);
    }

    @Override
    public Integer getCount() {
        return stationMasterMapper.getCount();
    }

    @Override
    public Integer getCountByStationName(String stationName) {
        return stationMasterMapper.getCountByStationName(stationName);
    }

    @Override
    public Integer getCountLikeNameOrPhone(String content) {
        content = content==null || "".equals(content) ? "%%" : "%"+content+"%";
        return stationMasterMapper.getCountLikeNameOrPhone(content);
    }

    @Override
    public boolean update(StationMasterVo stationMasterVo) {
        StationMaster stationMaster = new StationMaster();
        stationMaster.setId(stationMasterVo.getId());
        stationMaster.setPhone(stationMasterVo.getPhone());
        stationMaster.setName(stationMasterVo.getName());
        if (stationMasterVo.getPwd()!=null){
            stationMaster.setPwd(MD5Util.encode(stationMasterVo.getPwd()));
        }
        stationMaster.setRegTime(stationMasterVo.getRegTime());
        stationMaster.setStationId(stationMasterMapper.selectStationIdByName(stationMasterVo.getStationName()));
        return stationMasterMapper.update(stationMaster);
    }

    @Override
    public boolean del(Integer id) {
        return stationMasterMapper.del(id);
    }

    @Override
    public boolean add(StationMasterVo stationMasterVo) {
        StationMaster stationMaster = new StationMaster();
        stationMaster.setPhone(stationMasterVo.getPhone());
        stationMaster.setName(stationMasterVo.getName());
        stationMaster.setPwd(MD5Util.encode("123456"));
        stationMaster.setRegTime(new Timestamp(System.currentTimeMillis()));
        stationMaster.setStationId(stationMasterMapper.selectStationIdByName(stationMasterVo.getStationName()));
        return stationMasterMapper.insert(stationMaster);
    }

    @Override
    public StationMasterVo selectByPhone(String phone) {
        return stationMasterMapper.selectByPhone(phone);
    }

    @Override
    public StationMasterVo selectById(Integer id) {
        return stationMasterMapper.selectById(id);
    }

    @Override
    public StationMasterVo login(String phone, String pwd) {
        StationMasterVo stationMasterVo = stationMasterMapper.selectByPhone(phone);
        System.out.println("stationMasterVo = " + stationMasterVo);
        if (stationMasterVo.getPwd().equals(MD5Util.encode(pwd))){
            return stationMasterVo;
        }else {
            return null;
        }
    }

    @Override
    public StationMaster selectMasterAndMenuById(Integer id) {
        return stationMasterMapper.selectMasterAndMenuById(id);
    }
}
